﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NasuTek.Shion.Extencibility;
using System.Windows.Forms;
using NasuTek.Shion;

namespace Shion.WhoIsMenu
{
    public class Plugin : IPlugin
    {
        #region IPlugin Members

        public void Load(ExtencibilityManager e)
        {
            e.Plugins.Add("WhoIsPlugin", new ExtencibilityManager.PluginData("Who Is Menu Plugin",
                "(C) 2009 NasuTek Systems",
                "NasuTek Systems",
                "Allows you to whois a user using a menu",
                GetType().Assembly.GetName().Version));

            new WhoIsMenu(e);
        }

        #endregion
    }

    public class WhoIsMenu
    {
        ExtencibilityManager em;
        ToolStripMenuItem user = new ToolStripMenuItem("User: Getting Data");
        ToolStripMenuItem server = new ToolStripMenuItem("Server: Getting Data");
        ToolStripMenuItem idle = new ToolStripMenuItem("Idle: Getting Data");
        ToolStripMenuItem channels = new ToolStripMenuItem("Channels: Getting Data");

        public WhoIsMenu(ExtencibilityManager e)
        {
            em = e;
            ToolStripMenuItem whois = new ToolStripMenuItem("WhoIs");
            whois.DropDown.Items.Add(user);
            whois.DropDown.Items.Add(server);
            whois.DropDown.Items.Add(idle);
            whois.DropDown.Items.Add(channels);
            whois.DropDownOpening += new EventHandler(whois_DropDownOpening);
            em.NickMenu.Items.Add(whois);
        }

        void whois_DropDownOpening(object sender, EventArgs e)
        {
            em.WhoIsHandler += new ExtencibilityManager.WhoIsEventHandler(em_WhoIsHandler);
            em.WhoIsData.Handled = true;
            try
            {
                em.Clients[em.activeServer].RfcWhois(em.NickListView.SelectedItems[0].Text);
            }
            catch (ArgumentOutOfRangeException) { }
        }

        void em_WhoIsHandler(object sender)
        {
            PluginSupport.Invoke(new Action(delegate { user.Text = "User: " + em.WhoIsData.WhoIsUser; }));
            PluginSupport.Invoke(new Action(delegate { server.Text = "Server: " + em.WhoIsData.WhoIsServer; }));
            PluginSupport.Invoke(new Action(delegate { idle.Text = "Idle: " + em.WhoIsData.WhoIsIdle; }));
            PluginSupport.Invoke(new Action(delegate { channels.Text = "Channels: " + em.WhoIsData.WhoIsChannels; }));
            em.WhoIsData.CleanUpWhoIsData();
            em.WhoIsHandler -= new ExtencibilityManager.WhoIsEventHandler(em_WhoIsHandler);
        }
    }
}
